package onlinebanking.server.modules;



import java.sql.Timestamp;
import java.util.Calendar;
import java.util.GregorianCalendar;

import onlinebanking.constants.MSG;
import onlinebanking.exceptions.UserNotLoggedInException;
import onlinebanking.network.Message;
import onlinebanking.server.ServerEngine;
import onlinebanking.server.objects.Transaction;

public class ViewPendingTransactionModule extends Module {

	public ViewPendingTransactionModule(ServerEngine se, Message message) {
		this.serverengine = se;
		this.message = message;
	}

	public Message handleRequest() {
		Message response = new Message();
		if (!serverengine.isLoggedIn()) {
			response.setAsUserNotLoggedIn();
			return response;
		}
		
		int subtype = message.getMsgSubType();
		
		response.setMsgType(MSG.VIEW_TRANSACTIONS);
		response.setMsgSubType(subtype);
		
		
		switch (subtype) {
		case MSG.LIST_OF_PENDING_TRANSACTIONS:

			response = generatePendingTransactionList();
			return response;
		
		case MSG.DETAIL_SPECIFIC_TRANSACTION:
			
			int transactionid = message.getInt("transactionid");
			try {
				int personalaccountid = this.serverengine.getSession().getUserAccount().getpersonalaccountid();
			
				if(Transaction.ownsTransaction(transactionid, personalaccountid)){
					response = getTransactionDetails(transactionid);
					return response;
				}else{
					response.setSuccess(false);
					response.addAttribute("reason", "Transaction does not belong to you.");
					return response;
				}
				
			} catch (UserNotLoggedInException e) {
				return new Message();
			}
			
		default:
			return new Message();
		}
	}

	private Message generatePendingTransactionList() {
		
		try {
			int bankaccountid = serverengine.getSession()
					.getActiveBankAccount().getBankAccountID();

			Message response = Transaction
					.getPendingTransactions(bankaccountid);
			response.setMsgType(MSG.VIEW_TRANSACTIONS);
			response.setMsgSubType(MSG.LIST_OF_PENDING_TRANSACTIONS);

			return response;

		} catch (UserNotLoggedInException e) {
			// TODO Auto-generated catch block
			return new Message();
		}

	}
	
	private Message getTransactionDetails(int transactionid){
		Message response = new Message();
		response.setMsgType(MSG.VIEW_TRANSACTIONS);
		response.setMsgSubType(MSG.DETAIL_SPECIFIC_TRANSACTION);
		
		
		Transaction transaction =  new Transaction(transactionid);
		char transactiontypeid = transaction.getTransactiontypeid();
		char transactionstatusid = transaction.getTransactionstatusid();
		Timestamp expiry = transaction.getExpiration();
		Timestamp processedon = transaction.getProcessedon();
		
		GregorianCalendar gcal = (GregorianCalendar) Calendar.getInstance();
		gcal.setTime(expiry);
		int expDay = gcal.get(Calendar.DAY_OF_MONTH);
		int expMonth = gcal.get(Calendar.MONTH);
		int expYear = gcal.get(Calendar.YEAR);
		
		if(processedon != null ){
			gcal.setTime(processedon);			
			int procDay = gcal.get(Calendar.DAY_OF_MONTH);
			int procMonth = gcal.get(Calendar.MONTH);
			int procYear = gcal.get(Calendar.YEAR);
			response.addAttribute("procDay", procDay);
			response.addAttribute("procMonth", procMonth);
			response.addAttribute("procYear", procYear);
		}
		
		response.addAttribute("transactionowner", transaction.getTransactionOwner());
		
		
		response.addAttribute("transactiontypeid", transactiontypeid);
		response.addAttribute("transactiontype", Transaction.getTransactionTypeString(transactiontypeid));
		response.addAttribute("transactionstatusid", transactionstatusid);
		response.addAttribute("transactionstatus", Transaction.getTransactionStatusString(transactionstatusid));
		response.addAttribute("expDay", expDay);
		response.addAttribute("expMonth", expMonth);
		response.addAttribute("expYear", expYear);
		
		
		if(transactiontypeid == Transaction.FUNDS_TRANSFER){
			
			Message transferdetails = Transaction.getFundsTransferDetails(transactionid);
			
			double ammounttransfer = transferdetails.getDouble("ammounttransfer");
			int srcaccount = transferdetails.getInt("srcaccount");
			int destaccount = transferdetails.getInt("destaccount");;
			int srcbank = transferdetails.getInt("srcbank");;
			int destbank = transferdetails.getInt("destbank");;
			
			response.addAttribute("ammounttransfer", ammounttransfer);
			response.addAttribute("srcaccount", srcaccount);
			response.addAttribute("destaccount", destaccount);
			response.addAttribute("srcbank", srcbank);
			response.addAttribute("destbank", destbank);
		}
		
		
		return response;
		
	}
}
